public class DrawPictures {

	/**
	 * *
	 * 
	 * @param path
	 *            path to folder of PNG maps
	 * @param xSegs
	 *            number of segments in X axis
	 * @param ySegs
	 *            number of segments in Y axis
	 * @param depth
	 *            how deep to go in the directory
	 * @param xMapULCoord
	 *            x coordinate of upper left corner of the map
	 * @param yMapULCoord
	 *            y coordinate of upper left corner of the map
	 * @param wMap
	 *            the width of the map in meters
	 * @param hMap
	 *            the height of the map in meters
	 * @param xPixToMet
	 *            the ratio- number of pixels in a meter for x axis
	 * @param yPixToMet
	 *            the ratio- number of pixels in a meter for y axis
	 * @param wDisp
	 *            width of the display
	 * @param hDisp
	 *            height of the display
	 * @param xDispUL
	 *            x coordinate of upper left corner of the display
	 * @param yDispUL
	 *            y coordinate of upper left corner of the display
	 */

	public static void parentDrawPictures(String path, int xSegs, int ySegs,
			int depth, double xMapULCoord, double yMapULCoord, double wMap,
			double hMap, double xPixToMet, double yPixToMet, double wDisp,
			double hDisp, double xDispUL, double yDispUL) {

		double left, right, top, bottom;

		left = (xMapULCoord - xDispUL) * xPixToMet;
		right = (xMapULCoord + wMap - xDispUL) * xPixToMet;
		top = (yDispUL - yMapULCoord) * yPixToMet;
		bottom = (yDispUL - (yMapULCoord - hMap)) * yPixToMet;
		drawPictures(path, xSegs, ySegs, depth, left, top, right, bottom,
				wDisp, hDisp);

	}

	public static void drawPictures(String path, int xSegs, int ySegs,
			int depth, double left, double top, double right, double bottom,
			double wDisp, double hDisp) {

		for (int row = 0; row < ySegs; row++)
			for (int col = 0; col < xSegs; col++) {
				double leftOfSeg, rightOfSeg, topOfSeg, bottomOfSeg;
				leftOfSeg = (right - left) * col / xSegs + left;
				rightOfSeg = leftOfSeg + (right - left) / xSegs;
				topOfSeg = (top - bottom) * row / ySegs + top;
				bottomOfSeg = topOfSeg + (top - bottom) / ySegs;
				if (isRelevant(leftOfSeg, topOfSeg, rightOfSeg, bottomOfSeg,
						wDisp, hDisp))
					if (depth == 0) {
						drawPic(path + "\\" + col + "_" + row + ".png",
								leftOfSeg, topOfSeg, rightOfSeg, bottomOfSeg,
								wDisp, hDisp);

					} else {
						drawPictures(path + "\\" + col + "_" + row, xSegs,
								ySegs, depth - 1, leftOfSeg, topOfSeg,
								rightOfSeg, bottomOfSeg, wDisp, hDisp);

					}
			}
	}

	private static void drawPic(String pic, double leftOfSeg, double topOfSeg,
			double rightOfSeg, double bottomOfSeg, double wDisp, double hDisp) {
		double leftOfPic, topOfPic, picW, picH, xDrawAt,yDrawAt;
		leftOfPic = Math.max(0, leftOfSeg);
		topOfPic = Math.max(0, topOfSeg);
		picW = Math.min((rightOfSeg - leftOfSeg) - leftOfPic, wDisp);
		picH = Math.min((bottomOfSeg - topOfSeg) - topOfPic, hDisp);
		xDrawAt=Math.max(leftOfSeg, 0);
		yDrawAt=Math.max(topOfSeg, 0);
		
		
	}

	// row and col start from 0 to (numSegs-1)
	public static boolean isRelevant(double leftOfSeg, double topOfSeg,
			double rightOfSeg, double bottomOfSeg, double wDisp, double hDisp) {
		if (leftOfSeg > wDisp)
			return false;
		if (rightOfSeg < 0)
			return false;
		if (topOfSeg > hDisp)
			return false;
		if (bottomOfSeg < 0)
			return false;
		return true;
	}

}
